package com.controller;

import com.entity.RespObject;
import com.entity.UserTable;
import com.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: GuoTong
 * @createTime: 2021-06-23 10:25
 * @since JDK 1.8 OR 11
 **/
@RestController
@Slf4j
@Api(value = "SpringBoot-CRUD-Demo", tags = "基于SpringBoot的增删改查示例Controller")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * Description: http://localhost:8888/getList
     */
    @GetMapping(value = "/getList", name = "全部获取数据")
    @ApiOperation("全部获取数据-接口")
    public RespObject getListByNotLimit() {
        List<UserTable> users = userService.getUsers();
        return RespObject.respOk(users);
    }

    /**
     * Description: http://localhost:8888/getListById?id=1001
     */
    @GetMapping(value = "/getListById", name = "根据ID获取数据")
    @ApiOperation("根据ID获取数据-接口")
    public RespObject getUserTable(@RequestParam("id") int id) {
        UserTable user = userService.getUserById(id);
        if (user == null) {
            return RespObject.respNo("输入的ID无效");
        }
        return RespObject.respOk(user);
    }

    /**
     * Description:  http://localhost:8888/getUsersByLimit?currentPage=1&pageSize=10
     * select * from table limit (start-1)*pageSize,pageSize;
     * mysql 分页第一个参数是index{0-max}，第二个参数是取的数量
     */
    @GetMapping(value = "/getUsersByLimit", name = "分页查询")
    @ApiOperation("分页查询-接口")
    public RespObject getUserTableByLimit(@RequestParam("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        log.info("每一页分：" + pageSize + "条" + "；请求的第：" + currentPage + "页");
        if (currentPage == 0) {
            return RespObject.respNo("首页坐标是从1开始");
        }
        List<UserTable> limitUsers = userService.getUserByLimit((currentPage - 1) * pageSize, pageSize);
        return RespObject.respOk(limitUsers);
    }

    /**
     * Description: 添加操作
     */
    @PostMapping(value = "/addUserTable", name = "添加表数据")
    @ApiOperation("添加表数据-接口")
    public RespObject addUserTableData(@RequestBody UserTable userTable) {
        System.out.println(userTable);
        int id = userTable.getId();
        //判断ID是否存在-{isPresent存在就返回true}
        if (id != 0) {
            return RespObject.respNo("添加操作不允许传递主键");
        }
        int rowChange = 0;
        try {
            rowChange = userService.insertInToUserTable(userTable);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowChange > 0 ? RespObject.respOk() : RespObject.respNo("添加失败");
    }

    /**
     * Description: 删除操作
     */
    @DeleteMapping(value = "/delUserTableById/{id}", name = "删除表数据")
    @ApiOperation("删除表数据-接口")
    public RespObject addUserTableData(@PathVariable("id") int id) {
        int rowChange = 0;
        try {
            rowChange = userService.deleteusertablebyid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowChange > 0 ? RespObject.respOk() : RespObject.respNo("该ID无效-删除失败");
    }

    /**
     * Description: 修改操作
     */
    @PutMapping(value = "/updateUserTableById", name = "修改表数据")
    @ApiOperation("修改表数据-接口")
    public RespObject updateUserTableData(@RequestBody UserTable userTable) {
        int id = userTable.getId();
        //判断-{isPresent存在就返回true}
        if (id == 0) {
            return RespObject.respNo("修改操作必须传递主键");
        }
        int rowChange = 0;
        try {
            rowChange = userService.updateUserTableById(userTable);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowChange > 0 ? RespObject.respOk() : RespObject.respNo("修改失败");
    }
}
